package com.ihg.hiex.admin.handler.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.utils.DateFormatUtils;
import com.ihg.hiex.admin.entity.dto.Competency;
import com.ihg.hiex.admin.entity.dto.Overall;
import com.ihg.hiex.admin.entity.dto.Swift;
import com.ihg.hiex.admin.handler.PDFHandler;
import com.ihg.hiex.admin.handler.support.PDFTemplate;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.before.delegatework.DelegateCareerGoals;
import com.ihg.hiex.entity.session.before.delegatework.DelegateWrokRecord;
import com.ihg.hiex.entity.session.during.SessionCalibrationCompetency;
import com.ihg.hiex.entity.session.during.report.SessionReportComment;
import com.ihg.hiex.entity.session.during.report.SessionReportRecommendation;
import com.ihg.hiex.entity.user.Assessor;
import com.ihg.hiex.entity.user.Delegate;
import com.itextpdf.text.DocumentException;

/**
 * 
 * @author houtao
 *
 */
@Component("pdfHandler")
public class PDFHandlerImpl implements PDFHandler {
    
    private static final Logger logger = LoggerFactory.getLogger(PDFHandlerImpl.class);
    
    @Value("#{config['cloud.path']}")
    private String cloudpath;
    
    @Value("#{cloud['cloud.host']}")
    private String cloudhost;
    
    @Value("#{config['font.path']}")
    private String fontPath;
    
    @Autowired
    private VelocityEngine velocityEngine;

    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.handler.PDFHandler#savaDelegateReport(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Assessor, com.ihg.hiex.entity.user.Delegate, java.util.List, com.ihg.hiex.entity.session.before.delegatework.DelegateCareerGoals, com.ihg.hiex.admin.entity.dto.Competency, com.ihg.hiex.entity.session.during.report.SessionReportComment)
     */
    public String savaDelegateReport(Session session, Assessor assessor, Delegate delegate, List<DelegateWrokRecord> wrokRecords, 
    		                         DelegateCareerGoals goals, Competency competency, SessionReportComment comment, Map<String, SessionCalibrationCompetency> sessionCalibrationCompetencies, boolean english) {
        
        try {
        	
        	logger.info("start save delegate report");
            
            Map<String, Object> model = new HashMap<String, Object>();
            
            model.put("assessor", assessor);
            model.put("delegate", delegate);
            model.put("wrokRecords", wrokRecords);
            model.put("goals", goals);
            model.put("ownerRelations", competency.getOwnerRelations());
            model.put("operationsManagement", competency.getOperationsManagement());
            model.put("revenueMaximisation", competency.getRevenueMaximisation());
            model.put("result", competency.getDriveResult());
            model.put("change", competency.getChampionChange());
            model.put("develop", competency.getLeadAndDevelop());
            model.put("collaboratively", competency.getWorkCollaboratively());
            model.put("management", competency.getFinancialManagement());
            model.put("comment", comment);
            model.put("competencies", sessionCalibrationCompetencies);
            
            if(english) {
            	model.put("date", DateFormatUtils.format(session.getStartDate(), "yyyy MM dd") + "——" + DateFormatUtils.format(session.getEndDate(), "MM dd"));
            	
            	logger.info("start save delegate report data alerday");
            	
            	PDFTemplate file = new PDFTemplate("delegate_report_en.pdf").initialize(velocityEngine, model, "content/delegate-report-en.vm");
            	
            	logger.info("start save delegate report file generated");
            	
            	String reportUrl = file.toPDF(this.cloudpath, session.getName() + "/" + delegate.getName() + "/report", this.cloudhost);
            	
            	logger.info("start save delegate report file url get [{}]", reportUrl);
            	
            	return reportUrl;
            } else {
            	
            	model.put("date", DateFormatUtils.format(session.getStartDate(), "yyyy年MM月dd日") + "——" + DateFormatUtils.format(session.getEndDate(), "MM月dd日"));
            	
            	logger.info("start save delegate report data alerday");
            	
            	PDFTemplate file = new PDFTemplate("delegate_report_zh.pdf").initialize(velocityEngine, model, "content/delegate-report-zh.vm", fontPath);
            	
            	logger.info("start save delegate report file generated");
            	
            	String reportUrl = file.toPDF(this.cloudpath, session.getName() + "/" + delegate.getName() + "/report", this.cloudhost);
            	
            	logger.info("start save delegate report file url get [{}]", reportUrl);
            	
            	return reportUrl;
            }
            
        } catch (IOException e) {
            logger.error(" savaPDF() - error is [{}]", e.getMessage());
            throw new BusinessException("save pdf failed");
        } catch (DocumentException e) {
            logger.error(" savaPDF() - error is [{}]", e.getMessage());
            throw new BusinessException("save pdf failed");
        }
        
    }

    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.handler.PDFHandler#savePeoplePlaningReport(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Delegate, com.ihg.hiex.admin.entity.dto.Swift, com.ihg.hiex.admin.entity.dto.Competency, com.ihg.hiex.admin.entity.dto.Overall, com.ihg.hiex.entity.session.during.report.SessionReportRecommendation)
     */
    public String savePeoplePlaningReport(Session session, Delegate delegate, Swift swift, Competency competency, Overall overall, SessionReportRecommendation recommendation) {
    	
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("delegate", delegate);
        model.put("ownerRelations", competency.getOwnerRelations());
        model.put("operationsManagement", competency.getOperationsManagement());
        model.put("revenueMaximisation", competency.getRevenueMaximisation());
        model.put("result", competency.getDriveResult());
        model.put("change", competency.getChampionChange());
        model.put("develop", competency.getLeadAndDevelop());
        model.put("collaboratively", competency.getWorkCollaboratively());
        model.put("management", competency.getFinancialManagement());
        model.put("overall", overall);
        model.put("swift", swift);
        model.put("recommendation", recommendation);
        
        // 1.  构建pdf模版,
        PDFTemplate file = new PDFTemplate("people_planing_report.pdf").initialize(velocityEngine, model, "content/people-planing-report.vm", fontPath);
        
        try {
            
            // 2. 保存到本地路径
            String reportUrl = file.toPDF(this.cloudpath, session.getName() + "/" + delegate.getName() + "/report", this.cloudhost);
            return reportUrl;
            
        } catch (IOException e) {
            
            logger.error(" savaPDF() - error is [{}]", e.getMessage());
            throw new BusinessException("save pdf failed");
            
        } catch (DocumentException e) {
            
            logger.error(" savaPDF() - error is [{}]", e.getMessage());
            throw new BusinessException("save pdf failed");
        }
    }
    
}
